{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "选择排序\n",
    "\n",
    "\n",
    "\n",
    "    1、在未排序序列中找到最小（大）元素，存放到排序序列的起始位置。\n",
    "    2、再从剩余未排序元素中继续寻找最小（大）元素，然后放到已排序序列的末尾。\n",
    "    3、以此类推，直到所有元素均排序完毕。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "def select_sort(nums):\n",
    "    n = len(nums)\n",
    "    \n",
    "    for i in range(n):\n",
    "        pos = i # 最小值的下标\n",
    "        for j in range(i+1, n):\n",
    "            if nums[j] < nums[pos]:\n",
    "                pos = j\n",
    "        nums[i], nums[pos] = nums[pos], nums[i]\n",
    "    return nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3, 4, 5, 6, 7, 8, 9]"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nums = [1,4,8,3,7,2,5,9,6]\n",
    "select_sort(nums)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "select_sort  0.0102465 ms\n"
     ]
    }
   ],
   "source": [
    "import timeit\n",
    "print(\"select_sort \",timeit.timeit(\"select_sort([1,4,8,3,7,2,5,9,6])\", setup=\"from __main__ import select_sort\", number=1000), \"ms\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
